﻿using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using System.Xml.Linq;

namespace EasyXaf.LowCode.WorkflowEditors.Models.Activities;

[DomainComponent]
[ImageName("WF_Switch")]
[XafDisplayName("分支(Switch)")]
[ActivityOrder(20)]
[ActivityGroup(ActivityGroupConsts.CommonGroupName)]
public class SwitchActivity : BranchesActivity
{
    public override string Color => "#D35400";

    public override string ImageName => "WF_Switch";

    public override bool HasDetailView => true;

    [XafDisplayName("表达式")]
    public string Expression
    {
        get => GetPropertyValue<string>();
        set => SetPropertyValue(value);
    }

    public SwitchActivity(IWorkflow workflow, ISequence sequence)
        : base(workflow, sequence)
    {
        Caption = "分支";
    }

    public override void OnCreated()
    {
        base.OnCreated();

        Branches.Add(new WhenBranch(Workflow, this).Created());
        Branches.Add(new DefaultBranch(Workflow, this).Created());
    }

    public override void WriteStateToXml(XElement element)
    {
        base.WriteStateToXml(element);

        SetXmlValue(element, nameof(Expression), Expression);
    }

    public override void ReadStateFromXml(XElement element)
    {
        base.ReadStateFromXml(element);

        Expression = GetXmlValue<string>(element, nameof(Expression));
    }
}
